﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MPMovieCatalog.Util;

namespace MPMovieCatalog.Forms
{
    public partial class FieldsForm : Form
    {
        public FieldsForm()
        {
            InitializeComponent();

            foreach (Field field in Fields.GetFields())
            {
                availFieldsListBox.Items.Add(field);
            }
        }

        public ArrayList SelectedFields
        {
            get
            {
                ArrayList fields = new ArrayList();
                foreach (Field field in selectedFieldsListBox.Items)
                {
                    fields.Add(field);
                }

                return fields;
            }

            set
            {
                foreach (Field field in value)
                {
                    availFieldsListBox.Items.Remove(field);
                    selectedFieldsListBox.Items.Add(field);
                }
            }
        }

        private void mapFieldButton_Click(object sender, EventArgs e)
        {
            if (availFieldsListBox.SelectedItems == null) return;
            foreach (Field field in availFieldsListBox.SelectedItems)
            {
                selectedFieldsListBox.Items.Add(field);
            }

            for (int i = availFieldsListBox.SelectedItems.Count - 1; i >= 0; i--)
            {
                availFieldsListBox.Items.Remove(availFieldsListBox.SelectedItems[i]);
            }
        }

        private void unmapFieldButton_Click(object sender, EventArgs e)
        {
            if (selectedFieldsListBox.SelectedItems == null) return;
            foreach (Field field in selectedFieldsListBox.SelectedItems)
            {
                availFieldsListBox.Items.Add(field);
            }

            for (int i = selectedFieldsListBox.SelectedItems.Count - 1; i >= 0; i--)
            {
                selectedFieldsListBox.Items.Remove(selectedFieldsListBox.SelectedItems[i]);
            }
        }

        private void upFieldButton_Click(object sender, EventArgs e)
        {
            if (selectedFieldsListBox.SelectedIndices != null && selectedFieldsListBox.SelectedIndices.Count > 0 && selectedFieldsListBox.SelectedIndices[0] > 0)
            {
                int selectedIndex = selectedFieldsListBox.SelectedIndices[0];
                object selectedItem = selectedFieldsListBox.Items[selectedIndex];
                selectedFieldsListBox.Items.RemoveAt(selectedIndex);
                selectedFieldsListBox.Items.Insert(selectedIndex - 1, selectedItem);
            }
        }

        private void downFieldButton_Click(object sender, EventArgs e)
        {
            if (selectedFieldsListBox.SelectedIndices != null && selectedFieldsListBox.SelectedIndices.Count > 0 && selectedFieldsListBox.SelectedIndices[0] < selectedFieldsListBox.Items.Count - 1)
            {
                int selectedIndex = selectedFieldsListBox.SelectedIndices[0];
                object selectedItem = selectedFieldsListBox.Items[selectedIndex];
                selectedFieldsListBox.Items.RemoveAt(selectedIndex);
                selectedFieldsListBox.Items.Insert(selectedIndex + 1, selectedItem);
            }
        }
    }
}
